package cn.wolfcode.ssm.web.controller;

import cn.wolfcode.ssm.domain.SystemDictionary;
import cn.wolfcode.ssm.qo.JsonResult;
import cn.wolfcode.ssm.qo.QueryObject;
import cn.wolfcode.ssm.service.ISystemDictionaryService;
import cn.wolfcode.ssm.util.RequireAnnotation;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/systemDictionary")
public class SystemDictionaryController {

    @Autowired
    private ISystemDictionaryService systemDictionaryServlet;

    @RequireAnnotation(name = "字典目录页面",expression = "systemDictionary:list")
    @RequestMapping("list")
    public String listAll(Model model, QueryObject qo){
        PageInfo<SystemDictionary> pageInfo = systemDictionaryServlet.query(qo);
        model.addAttribute("pageInfo",pageInfo);
        return "systemDictionary/list";
    }

    @RequireAnnotation(name = "字典目录删除",expression = "systemDictionary:delete")
    @RequestMapping("delete")
    public String delete(Long id){
        systemDictionaryServlet.deleteById(id);
        return "redirect:/systemDictionary/list";
    }

    @RequireAnnotation(name = "字典目录编辑/保存",expression = "systemDictionary:saveOrUpdate")
    @RequestMapping("input")
    public String input(Model model,Long id){

        if(id != null){
            SystemDictionary systemDictionary = systemDictionaryServlet.selectById(id);
            model.addAttribute("systemDictionary",systemDictionary);
        }
        return "systemDictionary/input";
    }

    @RequireAnnotation(name = "字典目录编辑/保存",expression = "systemDictionary:saveOrUpdate")
    @RequestMapping("saveOrUpdate")
    @ResponseBody
    public JsonResult saveOrUpdate(Model model, SystemDictionary systemDictionary){

        if(systemDictionary.getId() != null){
            systemDictionaryServlet.updateById(systemDictionary);
        }else {
            systemDictionaryServlet.save(systemDictionary);
        }
        return new JsonResult();
    }

}
